package com.sudichina.sudichina.view.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class SideLetterBar extends View {
    private static final String[] Cheeses = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private Paint paint;
    private int ceheight;
    private int width;
    private int currentIndex = -1;
    private onIndexChangedListener Listener;

    public SideLetterBar(Context context) {
        this(context, null);
    }

    public SideLetterBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SideLetterBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        //创建画笔

        paint = new Paint();
        //颜色
        paint.setColor(Color.GRAY);
        //加粗
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextSize(35);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        ceheight = getHeight() / Cheeses.length;
        width = getWidth();
        //   canvas.drawText("#", 10f, 40f, paint);
        //遍历元素,,获取要显示的字母,然后获取宽度,高度 确定具体的位置
        for (int i = 0; i < Cheeses.length; i++) {
            String s = Cheeses[i];


            Rect rect = new Rect();
            paint.getTextBounds(s, 0, 1, rect);


            float x = rect.width();

            float y = rect.height();


            canvas.drawText(s, width / 2 - x / 2, ceheight / 2 + y / 2 + i * ceheight, paint);

        }


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:

                //计算位置
                float eventY = event.getY();

                int index = (int) (eventY / ceheight);//点击的Y坐标/每个字母占用的高度.就是第几个
                if (currentIndex != index) {


                    currentIndex = index;

                    //如果点击的位置计算的索引超出了最大的索引就-1
                    if (index >= Cheeses .length - 1) {
                        index = Cheeses .length - 1;
                    }
                    String letter = Cheeses[index];
                    // System.out.println("触摸到了:" + letter);
                    /**
                     * 需要解决的是重复打印出索引的问题
                     * 解决思路 :
                     * 定义一个变量记录当前的点击的位置
                     * 下次进来先判断下索引相同吗:
                     * 如果相同就不进行操作
                     * 如果不一样的时候,再去进行判断索引的位置
                     *
                     */



                    if (Listener != null) {
                        Listener.onSettitng(letter);
                    }

                }

                break;
            case MotionEvent.ACTION_UP:

                if (Listener != null) {
                    Listener.cancle();
                }

                break;


        }


        return true;
    }


    //接口
    public interface onIndexChangedListener {

        void onSettitng(String s);

        void cancle();
    }

    public void setonIndexChangedListener(onIndexChangedListener Listener) {

        this.Listener = Listener;

    }

}
